package com.sihuatech.coreagent.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sihuatech.coreagent.core.communication.entity.CodeRelationship;
import com.sihuatech.coreagent.core.communication.entity.PageInfo;
import com.sihuatech.coreagent.core.communication.entity.RequestSetting;
import com.sihuatech.coreagent.core.communication.entity.Response;
import com.sihuatech.coreagent.mapper.CodeRelationshipMapper;
import com.sihuatech.coreagent.service.CodeRelationshipService;
import com.sihuatech.coreagent.until.Consts;

@RestController
@RequestMapping("coreagent/codeRelationship")
public class CodeRelationshipController {

	private static final Logger logger = LoggerFactory.getLogger(CodeRelationshipController.class);
	
	@Autowired
	private CodeRelationshipMapper codeRelationshipMapper; 
	
	@Autowired
	private CodeRelationshipService codeRelationshipService; 
	
	
	
	@PostMapping("/getAllCodeRelationByPage")
	public Object getAllCodeRelationByPage(@RequestParam(required = true) Integer size,
			@RequestParam(required = true) Integer currentPage ,@RequestBody CodeRelationship codeRelationship) {
		QueryWrapper<CodeRelationship> qw = new QueryWrapper<>();
		QueryWrapper<CodeRelationship> qw1 = new QueryWrapper<>();
		if(!StringUtils.isEmpty(codeRelationship.getProvinceCode())) {
			qw.eq("province_code", codeRelationship.getProvinceCode());
			qw1.eq("province_code", codeRelationship.getProvinceCode());
		}
		if(!StringUtils.isEmpty(codeRelationship.getPlatformType())) {
			qw.eq("platform_type", codeRelationship.getPlatformType());
			qw1.eq("platform_type", codeRelationship.getPlatformType());
		}
		if(!StringUtils.isEmpty(codeRelationship.getPlatformProvinceCode())) {
			qw.eq("platform_province_code", codeRelationship.getPlatformProvinceCode());
			qw1.eq("platform_province_code", codeRelationship.getPlatformProvinceCode());
		}
		
		 List<CodeRelationship> resultList = codeRelationshipMapper.selectList(qw.orderByDesc("id").last(" limit " + (currentPage - 1) * size + "," +  size));
		 int total = codeRelationshipMapper.selectCount(qw1); 
		PageInfo<CodeRelationship> pageInfo = new PageInfo<>(currentPage, size, total, resultList);
		return pageInfo;
	}
	
	@PostMapping("/saveCodeRelationship")
	public Response saveRequestSetting(@RequestBody CodeRelationship codeRelationship) {
		Response res = new Response();
		if (codeRelationship == null) {
			logger.error("需要保存的省份编码关系对象为空");
			res.failWithMessage("没有需要保存的省份编码关系");
			return res;
		}
		try {
			res = codeRelationshipService.saveCodeRelationship(codeRelationship, res);
		} catch (Exception e) {
			logger.error("保存省份编码关系对象异常",e);
			res.failWithMessage("保存省份编码关系对象异常");
		}
		return res;
	}
	
	@GetMapping("/infoCodeRelationshipById")
	public Response infoCodeRelationshipById(Long id) {
		Response res = new Response();
		 CodeRelationship codeRelationship = codeRelationshipMapper.selectById(id);
		if(codeRelationship == null) {
			res.failWithMessage("查询失败，id为 ：" + id);
			return res;
		}
		res.setCodeAndMessageAndData(Consts.SUCCESS, "查询成功", codeRelationship);
		return res;
	}
	
	@DeleteMapping("/deleteByIds")
	public Object deleteByIds(@RequestBody String[] ids) {
		Response res = new Response();
		if (ids == null || ids.length == 0) {
			res.failWithMessage("没有要删除的省份对应关系");
			return res;
		}
		try {
			boolean result = codeRelationshipService.deleteByIds(ids);
			if (!result) {
				res.failWithMessage("需要删除的省份对应关系不存在");
				return res;
			}
		} catch (Exception e) {
			logger.error("删除省份对应关系异常", e);
			res.failWithMessage("删除省份对应关系异常");
			return res;
		}
		res.setCodeAndMessage(Consts.SUCCESS, "删除成功");
		return res;
	}
}
